#include "Emitter.h"
#include "Particle.h"



CEmitter::CEmitter(void)
{

}
CEmitter::CEmitter(CParticle* prmParticle, bool prmLoop, SGD::Point prmPos, SGD::HTexture m_htexImage)
{}

CEmitter::~CEmitter()
{
	for (unsigned int i = 0; i < v_particles.size(); i++)
	{
		delete v_particles[i];
	}


	SGD::GraphicsManager::GetInstance()->UnloadTexture(m_htexImage);
}

void CEmitter::SetupParticle(unsigned int prmEmitType, int prmLoop,const char * prmFilePath)
{
	

	//OLD vvvv

	//loading stuff
	TiXmlDocument doc;


	// Attempt to load
	assert(doc.LoadFile(prmFilePath));
	TiXmlElement* pRoot = doc.RootElement();
	if (pRoot != nullptr)
	{
		TiXmlElement* xDetails = pRoot->FirstChildElement("Details");
		if (xDetails != nullptr)
		{
			double tempPrm;
			xDetails->Attribute("count", &m_iNumPart);
			tempPrm = m_iBurstLifetime;
			xDetails->Attribute("burstLifeTime", &tempPrm);
			xDetails->Attribute("loop", &isLooping);
			xDetails->Attribute("minLife", &m_MinLife);
			xDetails->Attribute("maxLife", &m_MaxLife);
			xDetails->Attribute("spawnRate", &m_SpawnRate);

			xDetails->Attribute("scale", &tempPrm);
			m_fScale = (float)tempPrm;
			xDetails = xDetails->NextSiblingElement("filePath");
			m_sFilePath = xDetails->GetText();

		}
	}

	switch (prmEmitType)
	{
	case CEmitter::STEADY:
	{
							 m_htexImage = SGD::GraphicsManager::GetInstance()->LoadTexture(m_sFilePath, SGD::Color(0, 0, 0));

							 float tempPrm;
							 //making particles
							 for (unsigned int i = 0; i < (unsigned int)m_iNumPart; i++)
							 {
								 if (m_SpawnRate != 0)
								 {
									 tempPrm = (float)(rand() % m_SpawnRate + 1);
								 }
								 else
								 {
									 tempPrm = 0;
								 }
								 //create particle with xml data.
								 CParticle * tempPar = new CParticle((rand() % m_MaxLife + m_MinLife),
									 SGD::Size(5, 5),
									 m_poiPos,
									 SGD::Vector((float)(rand() % 200), (float)(rand() % 200)),
									 SGD::Color(155, 155, 155),
									 1.0,
									 1.0,
									 0,
									 SGD::Vector(0.0f, 1.0f), 
									 tempPrm);
								 //placed in genric vector for keep-safe.
								 v_particles.push_back(tempPar);
								 //starting dead to later be spawned and placed in the living vector.
								 v_DeadPar.push_back(tempPar);
							 }
							 //ready for work.
							 m_bActive = true;


	}
		break;
	case CEmitter::BURST:
	{
							m_htexImage = SGD::GraphicsManager::GetInstance()->LoadTexture(m_sFilePath, SGD::Color(0, 0, 0));
							//making particles/storing
							for (unsigned int i = 0; i < (unsigned int)m_iNumPart; i++)
							{
								//create particle with xml data.
								CParticle * tempPar = new CParticle(m_iBurstLifetime,
									SGD::Size(5, 5),
									m_poiPos,
									SGD::Vector((float)(rand() % 100), (float)(rand() % 100)),
									SGD::Color(155, 155, 155),
									1.0,
									1.0,
									0,
									SGD::Vector(0.0, 1.0), (float)m_SpawnRate);

								//placed in genric vector for keep-safe.
								v_particles.push_back(tempPar);
								//starting dead to later be spawned and placed in the living vector.
								v_DeadPar.push_back(tempPar);
							}
							//ready for work.
							m_bActive = true;

	}
		break;
	default:
		break;
	}
	if (isLooping > 0)
	{
		isLooping = m_iNumPart;
	}
	//OLD ^^^^


}
void CEmitter::Update(float _elapsedTime)
{

	//OLD vvvv
	m_poiPos.x = m_OwnerPoint->x -50;
	m_poiPos.y = m_OwnerPoint->y;

	//if we have looped enough times, we are done
	if (isLooping < 0)
	{
		m_bActive = false;
	}

	//Is the Emitter even 'on' anymore?
	if (m_bActive)
	{

		for (unsigned int i = 0; i < v_particles.size(); i++)
		{
			v_particles[i]->Update(_elapsedTime, m_poiPos);
			//alive?
			if (v_particles[i]->m_bAlive)
			{
				for (unsigned int j = 0; j < v_DeadPar.size(); j++)
				{
					//is this you?
					if (v_DeadPar[j] == v_particles[i])
					{
						v_LivingPar.push_back(v_DeadPar[j]);
						v_DeadPar.erase(v_DeadPar.begin() + j);

						if (isLooping != 0)
						{
							//decrement loop counter.
							isLooping--;
						}
						break;
					}
				}
			}
			else//so you are dead.
			{
				for (unsigned int j = 0; j < v_LivingPar.size(); j++)
				{
					//is this you?
					if (v_LivingPar[j] == v_particles[i])
					{
						v_DeadPar.push_back(v_LivingPar[j]);
						v_LivingPar.erase(v_LivingPar.begin() + j);
						break;
					}
				}
			}
		}
	}
	//OLD ^^^^

}
void CEmitter::Render()
{
	if (m_bActive)
	{
		//Drawing Particles
		for (unsigned int i = 0; i < v_LivingPar.size(); i++)
		{
			SGD::GraphicsManager::GetInstance()->DrawTexture(this->m_htexImage, v_LivingPar[i]->m_poiPos
			, 0.0f, {}, {}, { m_fScale, m_fScale });
		}
	}
}

void CEmitter::LoadXML()
{

}